package com.jsnu.xiaochi.controller;

import com.alibaba.fastjson.JSON;
import com.jsnu.xiaochi.pojo.Category;
import com.jsnu.xiaochi.pojo.Food;
import com.jsnu.xiaochi.service.AdminService;
import com.jsnu.xiaochi.service.MyService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class AdminController {
    @Resource
    AdminService adminService;
    @Resource
    MyService myService;
    @Value("${NGINX_SERVER}")
    private String NGINX_SERVER;

    @RequiresRoles("admin")
    @RequestMapping("admin")
    public String showAdmin(Model model,HttpSession session){
        model.addAttribute("categorys",adminService.getAllCategory());
        if(session.getAttribute("user")!=null){
            return "admin";
        }
        return "404";
    }

    @RequiresPermissions("food:pass")
    @RequestMapping("getNopassFood")
    @ResponseBody
    public String getNopassFood(){
        List<Food> foods=adminService.getNoPassFood();
        Map map = new HashMap();
        map.put("data",foods);
        return JSON.toJSONString(map);
    }

    @RequestMapping("getPassFood")
    @ResponseBody
    public String getPassFood(){
        List<Food> foods=adminService.getPassFood();
        Map map = new HashMap();
        map.put("data",foods);
        return JSON.toJSONString(map);
    }

    @RequiresPermissions("food:pass")
    @RequestMapping("goPass")
    @ResponseBody
    public String goPass(String id){
        adminService.goPass(id);
        return "success";
    }

    @RequiresPermissions("category:delete")
    @RequestMapping("deleteCategory")
    @ResponseBody
    public String deleteCategory(int id){
        adminService.deleteCategory(id);
        return "success";
    }

    @RequiresPermissions("category:update")
    @RequestMapping("editCategory")
    @ResponseBody
    public String editCategory(int id,String name){
        Category category=new Category();
        category.setId(id);
        category.setCategoryName(name);
        adminService.editCategory(category);
        return "success";
    }

    @RequiresPermissions("category:insert")
    @RequestMapping("insertCategory")
    @ResponseBody
    public String insertCategory(String name){
        Category category=new Category();
        category.setCategoryName(name);
        adminService.insertCategory(category);
        return "success";
    }

    @RequiresPermissions("food:update")
    @RequestMapping("updateFood")
    @ResponseBody
    public String updateFood(String id,String name,String category,String intro,MultipartFile img) throws IOException {
        Food food=new Food();
        food.setId(id);
        food.setName(name);
        food.setCategory(category);
        food.setIntro(intro);
        if(img!=null){
            img.transferTo(new File(NGINX_SERVER+"\\xiaochi\\foodImg\\"+id+".jpg"));
        }
        adminService.updateFood(food);
        return "success";
    }

    @RequiresPermissions("food:delete")
    @RequestMapping("deleteFood")
    @ResponseBody
    public String deleteFood(String id){
        adminService.deleteFood(id);
        File img = new File(NGINX_SERVER+"\\xiaochi\\foodImg\\"+id+".jpg");
        img.delete();
        return "success";
    }
}
